<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MAC 前缀查询</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f4f4f4;
        }

        .container {
            width: 90%;
            max-width: 400px;
            background-color: #fff;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        h1 {
            font-size: 24px;
            text-align: center;
            margin-bottom: 20px;
        }

        .input-container {
            margin-bottom: 20px;
        }

        input[type="text"] {
            width: 100%;
            padding: 10px;
            font-size: 16px;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-sizing: border-box;
        }

        button {
            width: 100%;
            padding: 10px;
            font-size: 16px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }

        .result {
            margin-top: 20px;
            padding: 10px;
            background-color: #f1f1f1;
            border-radius: 5px;
            display: none;
        }

        .error {
            color: red;
            display: none;
        }

        .back-btn {
            margin-top: 20px;
            width: 100%;
            padding: 10px;
            background-color: #6c757d;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        .back-btn:hover {
            background-color: #5a6268;
        }
    </style>
</head>
<body>

<div class="container">
    <h1>MAC 前缀查询</h1>

    <div class="input-container">
        <input type="text" id="macPrefix" placeholder="请输入 MAC 前缀 (例如 XX:XX:XX)">
    </div>
    <button onclick="searchMac()">查询</button>

    <!-- 返回按钮 -->
    <button class="back-btn" onclick="window.history.back()">返回首页</button>


    <!-- 查询结果展示 -->
    <div class="result" id="result">
        <p id="resultMacPrefix"></p>
        <p id="resultType"></p>
    </div>

    <!-- 错误消息展示 -->
    <p class="error" id="error"></p>
</div>

<script>
    function searchMac() {
        // 获取输入的mac前缀
        const macPrefix = document.getElementById('macPrefix').value.trim();

        // 如果输入为空，显示错误消息
        if (!macPrefix) {
            showError('请输入MAC前缀！');
            return;
        }

        fetch(`/search?mac_prefix=${macPrefix}`)
                .then(response => response.json())
                .then(data => {
                    // 如果查询成功，展示结果
                    if (data.mac_prefix) {
                        showResult(data.mac_prefix, data.type);
                    } else if (data.message) {
                        // 如果查询失败，显示错误信息
                        showError(`${macPrefix} 不在告警库中！`);
                    }
                })
                .catch(error => {
                    showError('查询失败，请稍后重试！');
                    console.error('Error:', error);
                });
    }

    function showResult(macPrefix,type) {
        // 隐藏错误信息
        document.getElementById('error').style.display = 'none';

        // 显示查询结果
        document.getElementById('resultMacPrefix').textContent = `MAC 前缀：${macPrefix}`;
        document.getElementById('resultType').textContent = `类型：${type}`;
        document.getElementById('result').style.display = 'block';
    }

    function showError(message) {
        // 隐藏查询结果
        document.getElementById('result').style.display = 'none';

        // 显示错误信息
        document.getElementById('error').textContent = message;
        document.getElementById('error').style.display = 'block';
    }
</script>

</body>
</html>
